<?
require_once('class/conexion/conexion.php');
require_once('database/DBNodo.php');
require_once('database/DBEnlace.php');
require_once('database/DBValorAtributo.php');


class DBRed
{

	public $mConexion = null;

	public function __construct() 
	{
	   $this->mConexion = new Conexion($this);
	}
		
	public function nuevaRed($nombreRed)
	{
		$sql ="INSERT INTO `red_social` (`NOMBRE`) VALUES ('".mysql_real_escape_string($nombreRed)."')";
		$this->mConexion->sql($sql);
		
		return true;
	}
	
	public function mostrarRed($id)
	{
	    $sql =" SELECT * FROM RED_SOCIAL WHERE ID = ".mysql_real_escape_string($id)."";
	    return  mysql_fetch_array($this->mConexion->sql($sql));
	    
	}
	
	public function seleccionarRed($nombreRed)
	{
		$sql =" SELECT * FROM RED_SOCIAL WHERE NOMBRE = '".mysql_real_escape_string($nombreRed)."'";
		return $this->mConexion->sql($sql);
	}
	
	public function Red($idRed)
	{
		$sql =" SELECT * FROM RED_SOCIAL WHERE ID = '".mysql_real_escape_string($idRed)."'";
		return $this->mConexion->sql($sql);
	}
	
		
	public function validarNombreRedSocial($nombre)
	{
		$sql ="SELECT COUNT(*) FROM RED_SOCIAL WHERE NOMBRE = '".mysql_real_escape_string($red)."'";
		$total = mysql_fetch_array($this->mConexion->sql($sql));

		return $total == 0;
	}
	
	public function listarRedes()
	{
		$sql ="SELECT * FROM RED_SOCIAL";
		return $this->mConexion->sql($sql);
	}
	
	public function borrarRed($idRed)
	{
		$sql[] = "DELETE FROM VALOR_ATRIBUTO 
					WHERE ( 
						VALOR_ATRIBUTO.TIPO_ORIGEN =  'NODO' 
						AND VALOR_ATRIBUTO.ID_ORIGEN 
						IN ( 
							SELECT ID 
							FROM NODO 
							WHERE ID_RED = ".$idRed." 
						) 
					) 
					OR ( 
					VALOR_ATRIBUTO.TIPO_ORIGEN =  'ENLACE' 
					AND VALOR_ATRIBUTO.ID_ORIGEN IN ( 
						SELECT ID 
						FROM ENLACE 
						WHERE ENLACE.ID_ORIGEN IN ( 
							SELECT ID 
							FROM NODO 
							WHERE ID_RED =".$idRed." 
							) 
						)
					)";
		
		$sql[] = "DELETE FROM ENLACE WHERE ENLACE.ID_ORIGEN IN ( 
							SELECT ID 
							FROM NODO 
							WHERE ID_RED =".$idRed." 
							) ";
		
		$sql[] = "DELETE FROM NODO WHERE ID_RED = ".$idRed;
		
	    $sql[] = "DELETE FROM RED_SOCIAL WHERE ID= '".mysql_real_escape_string($idRed)."'";
	    
		return $this->mConexion->sql($sql);
		
	}

	public function incrementarTiempo($idRed) 
	{
		$sql[]= "UPDATE RED_SOCIAL SET TIEMPO = TIEMPO + 1 WHERE ID = ".mysql_real_escape_string($idRed);
		$redes =  $this->mostrarRed($idRed);
		$tiempo_old = $redes['TIEMPO'];
		
		$sql[] = "INSERT INTO NODO (ID, ID_RED, NOMBRE, BORRADO, TIEMPO)
					SELECT ID, ID_RED, NOMBRE, BORRADO, TIEMPO + 1 
					FROM NODO 
					WHERE ID_RED = ".mysql_real_escape_string($idRed)."
					AND TIEMPO = ".mysql_real_escape_string($tiempo_old);
		
		$sql[] = "INSERT INTO ENLACE (ID, ID_ORIGEN, ID_DESTINO, TIEMPO)
					SELECT ID, ID_ORIGEN, ID_DESTINO, TIEMPO + 1 
					FROM ENLACE 
					WHERE ID_ORIGEN IN (
										SELECT ID 
										FROM NODO 
										WHERE ID_RED = ".mysql_real_escape_string($idRed)." 
										AND TIEMPO = ".mysql_real_escape_string($tiempo_old).")
					AND ID_DESTINO IN (
										SELECT ID 
										FROM NODO 
										WHERE ID_RED = ".mysql_real_escape_string($idRed)." 
										AND TIEMPO = ".mysql_real_escape_string($tiempo_old).")
					AND TIEMPO = ".mysql_real_escape_string($tiempo_old);
		
		$sql[] = "INSERT INTO VALOR_ATRIBUTO (ID_ORIGEN, TIPO_ORIGEN, ID_ATRIBUTO, VALOR, TIEMPO)
					SELECT ID_ORIGEN, TIPO_ORIGEN, ID_ATRIBUTO, VALOR, TIEMPO + 1 
					FROM VALOR_ATRIBUTO 
					WHERE ID_ORIGEN IN 
						(SELECT ID FROM ENLACE WHERE ID_ORIGEN IN 
							(SELECT ID FROM NODO WHERE ID_RED = ".mysql_real_escape_string($idRed)."
							 AND TIEMPO = ".mysql_real_escape_string($tiempo_old).") )
							AND TIPO_ORIGEN = 'ENLACE' 
							AND TIEMPO = ".mysql_real_escape_string($tiempo_old)."  
						OR ID_ORIGEN IN 
							(SELECT ID FROM NODO WHERE ID_RED = ".mysql_real_escape_string($idRed)."
							 AND TIEMPO = ".mysql_real_escape_string($tiempo_old).")
							AND TIPO_ORIGEN = 'NODO' 
							AND TIEMPO = ".mysql_real_escape_string($tiempo_old)." ";
		
		$this->mConexion->sql($sql);
	}
	
	
	public function decrementarTiempo($idRed) 
	{
		$sql[]= "UPDATE RED_SOCIAL SET TIEMPO = TIEMPO - 1 WHERE ID = ".mysql_real_escape_string($idRed);
		$redes = $this->mostrarRed($idRed);	
		$tiempo = $redes['TIEMPO'];
		
		$sql[] = "DELETE FROM VALOR_ATRIBUTO
					WHERE
						(ID_ORIGEN IN 
							(SELECT ID FROM NODO WHERE ID_RED = ".mysql_real_escape_string($idRed).") 
							AND TIPO_ORIGEN = 'NODO' AND TIEMPO = ".mysql_real_escape_string($tiempo).")
						OR 
						(ID_ORIGEN IN 
							(SELECT ID FROM ENLACE WHERE ID_ORIGEN IN 
								(SELECT ID FROM NODO WHERE ID_RED = ".mysql_real_escape_string($idRed)."))
							
						AND TIPO_ORIGEN = 'ENLACE' 
						AND TIEMPO = ".mysql_real_escape_string($tiempo).")";
		
		$sql[] = "DELETE FROM ENLACE
					WHERE 
						ID_ORIGEN IN 
							(SELECT ID FROM NODO WHERE ID_RED = ".mysql_real_escape_string($idRed).")
						AND TIEMPO = ".mysql_real_escape_string($tiempo)."";
						
		$sql[] = "DELETE FROM NODO
					WHERE 
						ID_RED = ".mysql_real_escape_string($idRed)."
						AND TIEMPO = ".mysql_real_escape_string($tiempo)."";
							
		$this->mConexion->sql($sql);
		return $valor;
		
	}
	
	
	public function tiempo($idRed)
	{
		$sql=" SELECT MAX(TIEMPO) AS MAXTIEMPO FROM RED_SOCIAL WHERE ID = '".mysql_real_escape_string($idRed)."'";
		
		$tiempo=mysql_fetch_array($this->mConexion->sql($sql));
		
		return $tiempo['MAXTIEMPO'];
	}
	
		
		

	

}
?>